#include "stdafx.h"
#include "flydx9.h"
#include "DataController.h"
#include "SpriteStretch.h"
#include "..\LogicSprites\BaseLogicSprite.h"
#include "..\LogicSprites\LgSpriteStatic.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

SpriteStretch::SpriteStretch(LPDIRECT3DTEXTURE9 tex, int nWidth,int nHeight,int nLayer,bool bDrawOnView):
BaseDispSprite(nLayer,bDrawOnView,false),m_tex(tex),m_nWidth(nWidth),m_nHeight(nHeight)
{
	m_vStretchEndPos=D3DXVECTOR2(0.0f,0.0f);
}

SpriteStretch::~SpriteStretch()
{

}

bool SpriteStretch::World2ViewWithJudge( D3DXVECTOR3 &vPosition, D3DXVECTOR3 vCenter, int nWidth, int nHeight )
{
	if (!m_bDrawOnView){
		DataController::World2View(vPosition.x,vPosition.y);
		if( vPosition.x-vCenter.x>RESOLUTION_x || vPosition.x+nWidth-vCenter.x<0||
			vPosition.y-vCenter.y>RESOLUTION_y || vPosition.y+nHeight-vCenter.y<0){
				return false;
		}
	}
	return true;
}
bool SpriteStretch::Draw(DWORD dwCurrentTime)
{
	if(!BaseDispSprite::UpdatePosByLgSprite()) return false;

	D3DXVECTOR3 vCenter( m_vCenter.x,m_vCenter.y, 0.0f );   
	D3DXVECTOR3 vPosition( m_vPosition.x, m_vPosition.y, 0.0f );

		if(!World2ViewWithJudge(vPosition, vCenter,m_nWidth,m_nHeight)){
			return false;
		}

		if(m_tex){
			D3DXMATRIX ori;
			m_pD3DSprite->GetTransform(&ori);
			double sx,sy;
			sx=(m_vStretchEndPos.x-vPosition.x-vCenter.x)/m_nWidth;
			sy=(m_vStretchEndPos.y-vPosition.y-vCenter.y)/m_nHeight;
			D3DXMATRIX scale(
				sx,              0.0f,            0.0f,            0.0f,
				0.0f,            sy,              0.0f,            0.0f,
				0.0f,            0.0f,            1.0f,            0.0f,
				0.0f,            0.0f,            0.0f,            1.0f
			);
			D3DXMATRIX move;
			D3DXMatrixTranslation(&move,-vPosition.x,-vPosition.y,-vPosition.z);
			D3DXMatrixMultiply(&scale,&move,&scale);
			D3DXMatrixTranslation(&move,vPosition.x,vPosition.y,vPosition.z);
			D3DXMatrixMultiply(&scale,&scale,&move);

			m_pD3DSprite->SetTransform(&scale);
			m_pD3DSprite->Draw( m_tex, NULL, &vCenter, &vPosition, 
				D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f));
			m_pD3DSprite->SetTransform(&ori);
		}else{
#ifdef TEST_SFP
			//for testing
			RECT rc={vPosition.x-m_vCenter.x,vPosition.y-m_vCenter.y,vPosition.x-m_vCenter.x+m_nWidth,vPosition.y-m_vCenter.y+m_nHeight};
			m_pStatsFont->DrawText(m_pD3DSprite,m_csStr.GetBuffer(MAX_PATH),-1, &rc, DT_NOCLIP, D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));
			m_csStr.ReleaseBuffer();

			D3DXVECTOR2 pVertexList[5];//=new D3DXVECTOR2[5];
			pVertexList[0].x=rc.left;
			pVertexList[0].y=rc.top;
			pVertexList[1].x=rc.right;
			pVertexList[1].y=rc.top;
			pVertexList[2].x=rc.right;
			pVertexList[2].y=rc.bottom;
			pVertexList[3].x=rc.left;
			pVertexList[3].y=rc.bottom;
			pVertexList[4].x=rc.left;
			pVertexList[4].y=rc.top;
			m_pLine->Begin();
			m_pLine->Draw(pVertexList,5,D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));
			m_pLine->End();
#endif
		}
	
#ifdef TEST_COLLAPSE
	//for testing
	if (m_pLgSprite){
		vector<RectInBalloonElemnet_t> listHitRect;
		m_pLgSprite->GetHitRect(listHitRect);

		vector<RectInBalloonElemnet_t>::const_iterator it;
		for (it=listHitRect.begin();it!=listHitRect.end();it++){
			RECT rc;
			rc.left=vPosition.x+it->rect.left;
			rc.right=vPosition.x+it->rect.right;
			rc.top=vPosition.y+it->rect.top;
			rc.bottom=vPosition.y+it->rect.bottom;

			D3DXVECTOR2 pVertexList[5];//=new D3DXVECTOR2[5];
			pVertexList[0].x=rc.left;
			pVertexList[0].y=rc.top;
			pVertexList[1].x=rc.right;
			pVertexList[1].y=rc.top;
			pVertexList[2].x=rc.right;
			pVertexList[2].y=rc.bottom;
			pVertexList[3].x=rc.left;
			pVertexList[3].y=rc.bottom;
			pVertexList[4].x=rc.left;
			pVertexList[4].y=rc.top;
			m_pLine->Begin();
			m_pLine->Draw(pVertexList,5,D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));
			m_pLine->End();
		}

		RECT rc={vPosition.x-m_vCenter.x,vPosition.y-m_vCenter.y,vPosition.x-m_vCenter.x+m_nWidth,vPosition.y-m_vCenter.y+m_nHeight};
		CString cs;
		cs.Format(_T("%d"),m_pLgSprite->m_nIdx);
		m_pStatsFont->DrawText(m_pD3DSprite,cs.GetBuffer(MAX_PATH),-1, &rc, DT_NOCLIP, D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));
		cs.ReleaseBuffer();
	}

	//for testing
	if (m_pLgSprite&&eStaticLgSprite==m_pLgSprite->GetClassTpye())
	{
		LgSpriteStatic* p=dynamic_cast<LgSpriteStatic*>(m_pLgSprite);
		list<POINT> listpt=p->GetPointList();
		D3DXVECTOR2* pVertexList=new D3DXVECTOR2[listpt.size()];
		list<POINT>::iterator it;
		int i=0;
		for (it=listpt.begin();it!=listpt.end();it++)
		{
			pVertexList[i].x=it->x;
			pVertexList[i].y=it->y;
			i++;
		}
		m_pLine->Begin();
		m_pLine->Draw(pVertexList,listpt.size(),D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));
		m_pLine->End();
		delete[] pVertexList;
	}
#endif

	return true;
}